﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WC.DL.DALC.IF;
using WC.BL.BE;
using System.Data;

namespace WC.DL.DALC.MSSQL
{
    public class PopUpDALC: IPopUpDALC
    {
        #region Singleton
        public static PopUpDALC Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (typeof(PopUpDALC))
                    {
                        if (_instance == null)
                        {
                            _instance = new PopUpDALC();
                        }
                    }
                }
                return _instance;
            }
        }

        private PopUpDALC() { }
        private static volatile PopUpDALC _instance = null;
        #endregion
        
        #region IPopUpDALC Members

        public List<PopUPBE> read(PopUPBE pObjPopUPBE)
        {

            DateTime fi = DateTime.Parse(pObjPopUPBE.Fechai);
            DateTime ff = DateTime.Parse(pObjPopUPBE.Fechaf);
            
            List<PopUPBE> lst = new List<PopUPBE>();
            DataSet ds = SqlHelper.ExecuteDataset(DALC.ConnStr, "usps_popups", pObjPopUPBE.Titulo,
                fi, ff,
                                                  //fi.Month + "/" + fi.Day + "/" + fi.Year,
                                                  //ff.Month + "/" + ff.Day + "/" + ff.Year,
                                                  pObjPopUPBE.Popup_id);
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                PopUPBE m = new PopUPBE();
                m.Popup_id = int.Parse(dt.Rows[i]["popup_id"].ToString());
                m.Ramo_id = dt.Rows[i]["ramo_id"].ToString();
                m.Titulo = dt.Rows[i]["titulo"].ToString();
                m.Imagen = dt.Rows[i]["imagen"].ToString();
                m.Fechai = (DateTime.Parse(dt.Rows[i]["fechai"].ToString())).ToShortDateString();
                m.Fechaf = (DateTime.Parse(dt.Rows[i]["fechaf"].ToString())).ToShortDateString();
                lst.Add(m);
            }
            return lst;
        }

        public PopUPBE read(int id)
        {
            PopUPBE r = new PopUPBE(); DateTime pfecha = DateTime.Today;
            DataSet ds = SqlHelper.ExecuteDataset(DALC.ConnStr, "usps_PopUPs", string.Empty,
                                                  pfecha, pfecha, id);
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                r.Popup_id = int.Parse( dt.Rows[i]["PopUP_id"].ToString());
                r.Ramo_id = dt.Rows[i]["ramo_id"].ToString();
                r.Titulo = dt.Rows[i]["titulo"].ToString();
                r.Imagen = dt.Rows[i]["imagen"].ToString();
                r.Fechai = (DateTime.Parse(dt.Rows[i]["fechai"].ToString())).ToShortDateString();
                r.Fechaf = (DateTime.Parse(dt.Rows[i]["fechaf"].ToString())).ToShortDateString();
            }
            return r;
        }

        public string write(PopUPBE pObjPopUpBE)
        {
            int retpar = 0; string retval = string.Empty;

            DateTime fi = DateTime.Parse(pObjPopUpBE.Fechai);
            DateTime ff = DateTime.Parse(pObjPopUpBE.Fechaf);
            
            if (pObjPopUpBE != null)
            {
                DataSet ds = SqlHelper.ExecuteDataset(DALC.ConnStr, "uspi_PopUps", pObjPopUpBE.Ramo_id,
                                                      pObjPopUpBE.Imagen,
                                                      fi.ToShortDateString(),
                                                      ff.ToShortDateString(),
                                                      pObjPopUpBE.Titulo, pObjPopUpBE.Popup_id);
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    retpar = int.Parse(dt.Rows[i]["id"].ToString());
                }
                if (retpar == 0)
                {
                    retval = "No se pudo registrar el PopUp. Favor, comuniquese con el administrador del sistema.";
                }
                else
                {
                    retval = retpar.ToString();
                }
            }
            return retval;
        }

        public string update(PopUPBE pObjPopUpBE)
        {

            DateTime fi = DateTime.Parse(pObjPopUpBE.Fechai);
            DateTime ff = DateTime.Parse(pObjPopUpBE.Fechaf);
            
            int retpar = 1; string retval = string.Empty;
            if (pObjPopUpBE != null)
            {
                retpar = SqlHelper.ExecuteNonQuery(DALC.ConnStr, "uspu_PopUps", pObjPopUpBE.Popup_id, pObjPopUpBE.Ramo_id,
                                                   pObjPopUpBE.Imagen,
                                                  fi.ToShortDateString(),
                                                  ff.ToShortDateString(), 
                                                  pObjPopUpBE.Titulo);
                if (retpar != 0)
                {
                    retval = "No se modificó el pop up. Comuniquese con el administrador del sistema.";
                }
                else
                {
                    retval = "Se modificó el pop up.";
                }
            }
            return retval;
        }

        public string delete(int pIdPopUp)
        {
            int retpar = 1; string retval = string.Empty;

                retpar = SqlHelper.ExecuteNonQuery(DALC.ConnStr, "uspd_delpopup", pIdPopUp);
                if (retpar != 0)
                {
                    retval = "No se creó eliminar el pop up.";
                }
                else
                {
                    retval = "Se eliminó el pop up.";
                }
            
            return retval;
        }

        public List<PopUPBE> popupsXramo(string id)
        {
            List<PopUPBE> lst = new List<PopUPBE>();
            DataSet ds = SqlHelper.ExecuteDataset(DALC.ConnStr, "usps_popupsXramo", id);
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                PopUPBE r = new PopUPBE();
                r.Popup_id = int.Parse(dt.Rows[i]["PopUP_id"].ToString());
                r.Ramo_id = dt.Rows[i]["ramo_id"].ToString();
                r.Titulo = dt.Rows[i]["titulo"].ToString();
                r.Imagen = dt.Rows[i]["imagen"].ToString();
                r.Fechai = (DateTime.Parse(dt.Rows[i]["fechai"].ToString())).ToShortDateString();
                r.Fechaf = (DateTime.Parse(dt.Rows[i]["fechaf"].ToString())).ToShortDateString();
                lst.Add(r);
            }
            return lst;
        }
        #endregion
    }
}
